package com.yyryyty.chapter1;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

public class FilteringApples {

    public static void main(String... args) {

        List<Apple> inventory = Arrays.asList(new Apple(80, "green"),
                new Apple(155, "green"),
                new Apple(155, "blue"),
                new Apple(175, "blue"),
                new Apple(175, "green"),
                new Apple(120, "red")

        );

//        System.out.println(filterGreenApples(inventory));
//        System.out.println(filterHeavyApples(inventory));
//        System.out.println(filterApples(inventory, FilteringApples::isGreenApple));
//        System.out.println(filterApples(inventory, FilteringApples::isHeavyApple));
//        System.out.println(filterApples(inventory, (Apple a) -> "green".equals(a.getColor())));
//        System.out.println(filterApples(inventory, (Apple a) -> a.getWeight() > 150));
//        System.out.println(filterApples(inventory, (Apple a) -> a.getWeight() <= 80 ||
//                "brown".equals(a.getColor())));
//        System.out.println(inventory.stream().filter(apple -> apple.getWeight() > 150)
//                .collect(toList()));
//        inventory.sort((Apple a1, Apple a2) -> a1.getWeight().compareTo(a2.getWeight()));
//        System.out.println(inventory);
//        new Thread(() -> System.out.println("Hello World")).start();
//        System.out.println(filterApples(inventory, apple -> "green".equals(apple.getColor())));
//        Comparator<Apple> c = (a1, a2) -> a1.getWeight().compareTo(a2.getWeight());
//        System.out.println(inventory.stream()
//                .sorted((a1, a2) -> a1.getWeight().compareTo(a2.getWeight()))
//                .collect(toList())
//        );

//        System.out.println(inventory);
//        inventory.sort(comparing(Apple::getWeight));

//        inventory.sort(comparing(Apple::getWeight));
//        System.out.println(inventory);
//        inventory.sort(comparing(Apple::getWeight)
//                .thenComparing(Apple::getColor));
//        System.out.println(inventory);
//        inventory.sort(comparing(Apple::getWeight).reversed());
//        System.out.println(inventory);
        System.out.println(filterApples(inventory,
                apple -> apple.getWeight() > 150

        ));
    }

    public static List<Apple> filterGreenApples(List<Apple> inventory) {
        List<Apple> result = new ArrayList<>();
        for (Apple apple : inventory) {
            if ("green".equals(apple.getColor())) {
                result.add(apple);
            }
        }
        return result;
    }

    public static List<Apple> filterHeavyApples(List<Apple> inventory) {
        List<Apple> result = new ArrayList<>();
        for (Apple apple : inventory) {
            if (apple.getWeight() > 150) {
                result.add(apple);
            }
        }
        return result;
    }

    public static boolean isGreenApple(Apple apple) {
        return "green".equals(apple.getColor());
    }

    public static boolean isHeavyApple(Apple apple) {
        return apple.getWeight() > 150;
    }

    public static List<Apple> filterApples(List<Apple> inventory, Predicate<Apple> p) {
        List<Apple> result = new ArrayList<>();
        for (Apple apple : inventory) {
            if (p.test(apple)) {
                result.add(apple);
            }
        }
        return result;
    }

    public static class Apple {
        private int weight = 0;
        private String color = "";

        public Apple(int weight, String color) {
            this.weight = weight;
            this.color = color;
        }

        public Integer getWeight() {
            return weight;
        }

        public void setWeight(Integer weight) {
            this.weight = weight;
        }

        public String getColor() {
            return color;
        }

        public void setColor(String color) {
            this.color = color;
        }

        public String toString() {
            return "Apple{" +
                    "color='" + color + '\'' +
                    ", weight=" + weight +
                    '}';
        }
    }

}
